/**
 * 
 */
package com.sf.osms.dep.processor.util;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sf.osms.dep.biz.IAgentConfigBiz;
import com.sf.osms.dep.biz.ICarCodeBiz;
import com.sf.osms.dep.biz.ICommonServiceBiz;
import com.sf.osms.dep.domain.Router;
import com.sf.osms.dep.processor.exception.ExceptionConstants;
import com.sf.osms.dep.processor.exception.RouterException;

/**
 * @author sfhq1469 路由数据校验类
 */
@Component
public class VerifyRouter {
	
	private static IAgentConfigBiz agentConfigBizImpl;
	private static ICommonServiceBiz commonService;
	private static ICarCodeBiz carCodeBiz;
	
	@Autowired
	public void setAgentConfigBizImpl(IAgentConfigBiz agentConfigBizImpl) {
		VerifyRouter.agentConfigBizImpl = agentConfigBizImpl;
	}

	@Autowired
	public void setCommonService(ICommonServiceBiz commonService) {
		VerifyRouter.commonService = commonService;
	}

	@Autowired
	public void setCarCodeBiz(ICarCodeBiz carCodeBiz) {
		VerifyRouter.carCodeBiz = carCodeBiz;
	}

	public static void commonVertify(Router router) throws RouterException {
		Integer eventCode = router.getEventCode();

		// 代理的eventCode不能为空
		if (null == eventCode) {

			throw new RouterException(
					ExceptionConstants.EVENTCODE_EMPTY_CODE,
					ExceptionConstants.EVENTCODE_EMPTY_INFO);
		}

		// 运单号不能为空
		if (StringUtils.isBlank(router.getSfWaybill())) {

			throw new RouterException(
					ExceptionConstants.WAYBILL_EMPTY_CODE,
					ExceptionConstants.WAYBILL_EMPTY_INFO);
		}
		// 扫描日期不能为空
		if (null == router.getBarDate()) {

			throw new RouterException(
					ExceptionConstants.BAR_DATE_EMPTY_CODE,
					ExceptionConstants.BAR_DATE_EMPTY_INFO);
		}
		// 扫描时间不能为空
		if (null == router.getBarTime()) {

			throw new RouterException(
					ExceptionConstants.BAR_TIME_EMPTY_CODE,
					ExceptionConstants.BAR_TIME_EMPTY_INFO);
		}
		// 网点号不能为空
		if (StringUtils.isEmpty(router.getNetNum())) {

			throw new RouterException(ExceptionConstants.NET_NUM_EMPTY_CODE,ExceptionConstants.NET_NUM_EMPTY_INFO);
		}
		// 操作员编号不能为空
		if (StringUtils.isEmpty(router.getOperatorId())) {

			throw new RouterException(ExceptionConstants.OPERATOR_NUM_EMPTY_CODE,ExceptionConstants.OPERATOR_NUM_EMPTY_INFO);
		}
		// 上传类型若为空 设置默认值
		if (router.getBarUploadTypeCode() == null) {
			router.setBarUploadTypeCode(RouterConstants.AGENT_BAR_UPLOAD_OPR_CODE); // 上传类型
		}

		List<String> agentConfigList = agentConfigBizImpl.querySFCode(router
				.getAgentCode());
		String code = eventCode + "";
		if (null != router.getReasonCode()) {
			code = eventCode + "," + router.getReasonCode();
		}
		boolean flag = true;
		for (String node : agentConfigList) {
			if (code.equals(node)) {
				flag = false;
			}
		}
		if (flag) {
		
			List<String> sfCodes = commonService.findAllSfCode();
			
			if(!sfCodes.contains(code)){
				throw new RouterException(ExceptionConstants.EVENT_CODE_NOT_BELONG_TO_SFCODE_CODE,ExceptionConstants.EVENT_CODE_NOT_BELONG_TO_SFCODE_INFO);
			}
		}
		// 50节点的特殊字段校验
		if (eventCode == 50) {
			if (StringUtils.isEmpty(router.getOtherInfo())) {

				throw new RouterException(ExceptionConstants.PRODUCT_TYPE_EMPTY_CODE,ExceptionConstants.PRODUCT_TYPE_EMPTY_INFO);
			}
			if (StringUtils.isEmpty(router.getPhone())) {

				throw new RouterException(ExceptionConstants.PHONE_EMPTY_CODE,ExceptionConstants.PHONE_EMPTY_INFO);
			}
			if (null == router.getSubbillPieceQty()) {
				throw new RouterException(ExceptionConstants.SUBILL_QTY_EMPTY_CODE,ExceptionConstants.SUBILL_QTY_EMPTY_INFO);
				
			}
			if (null == router.getPayFlg()) {

				throw new RouterException(ExceptionConstants.PAY_FLG_EMPTY_CODE,ExceptionConstants.PAY_FLG_EMPTY_INFO);
			}
			if (null == router.getWeightQty()) {
				throw new RouterException(ExceptionConstants.WEIGHT_QTY_EMPTY_CODE,ExceptionConstants.WEIGHT_QTY_EMPTY_INFO);
				
			}
			if (StringUtils.isEmpty(router.getPhoneZone())) {
				throw new RouterException(ExceptionConstants.PHONE_ZONE_EMPTY_CODE,ExceptionConstants.PHONE_ZONE_EMPTY_INFO);
				
			}
		}
		// 51节点的特殊字段校验
		if (eventCode == 51) {
			if (StringUtils.isNotEmpty(router.getSfWaybill())) {
				router.setOpAttachInfo(router.getSfWaybill()); // 运单号 母单号一致
			}
			if (StringUtils.isEmpty(router.getPhoneZone())) {

				throw new RouterException(ExceptionConstants.PHONE_ZONE_EMPTY_CODE,ExceptionConstants.PHONE_ZONE_EMPTY_INFO);
			}
			if (StringUtils.isEmpty(router.getOtherInfo())) {

				throw new RouterException(ExceptionConstants.PRODUCT_TYPE_EMPTY_CODE,ExceptionConstants.PRODUCT_TYPE_EMPTY_INFO);
			}

		}
		// 30,31节点的特殊字段校验
		if (eventCode == 30 || eventCode == 31) {
			if (StringUtils.isEmpty(router.getContnrCode())) {
				
				String carnoType = carCodeBiz.queryCarCode(router
						.getAgentCode());
				String[] dateFormatMsg = carnoType.split(",");
				try {
					// 若不符合规格，则按照默认来来构造carno
					if (dateFormatMsg.length != 2) {
						dateFormatMsg = RouterConstants.AGENT_CARNO_TYPE
								.split(",");
					}
					router.setContnrCode("333"
							+ DateUtil.dateFomat(new Date(), dateFormatMsg[0])
							+ dateFormatMsg[1].replace("\\", ""));
				} catch (Exception e) {
					throw new RouterException(ExceptionConstants.CONT_CODE_ERROR_CODE,ExceptionConstants.CONT_CODE_ERROR_INFO);
				}

			}
			if (StringUtils.isEmpty(router.getOpAttachInfo())) {
				router.setOpAttachInfo("755A010A9999");
			}
			// 取车标号逻辑
			/*
			 * if (StringUtils.isEmpty(carnoType)) { if
			 * (StringUtils.isNotEmpty(router.getBatchCode())) { throw new
			 * Exception("Batch mismatch"); } else { if (listCarCode.size() ==
			 * 0) { throw new Exception("Batch mismatch"); }
			 * router.setContnrCode(listCarCode.get(0).getCarCode()); } } else {
			 * String[] dateFormatMsg = carnoType.split(","); try { //
			 * 若不符合规格，则按照默认来来构造carno if (dateFormatMsg.length != 2) {
			 * dateFormatMsg = RouterConstants.AGENT_CARNO_TYPE.split(","); }
			 * router.setContnrCode("333" + DateUtil.dateFomat(new Date(),
			 * dateFormatMsg[0]) + dateFormatMsg[1].replace("\\", "")); } catch
			 * (Exception e) { throw new
			 * Exception("The number of abnormal logo"); } }
			 */
		}
		// 33,44节点的特殊字段校验
		if (eventCode == 33 || eventCode == 44) {
			if (StringUtils.isEmpty(router.getCourierCode())) {
				router.setCourierCode(router.getOperatorId());
			}
		}
		if (router.getEventCode() == 33 || router.getEventCode() == 70) {
			if (router.getReasonCode() == null) {

				throw new RouterException(
						ExceptionConstants.REASONCODE_EMPTY_CODE,
						ExceptionConstants.REASONCODE_EMPTY_INFO);
			}
		}

		if (eventCode == 16 || eventCode == 14 || eventCode == 15
				|| eventCode == 18 || eventCode == 649 || eventCode == 44) {
			if (StringUtils.isEmpty(router.getBarSn())) { // 节点 16 14 15 18 649
															// 44 巴枪号
				router.setBarSn(RouterConstants.AGENT_BAR_SN_FILE); // 把枪设备号
			}
		} else {
			if (StringUtils.isEmpty(router.getBarSn())) {
				router.setBarSn(RouterConstants.AGENT_BAR_SN_FILE); // 系统标识
			}
		}
		
	}
}
